package com.lemon.soft.bookingroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lemon.soft.bookingroom.dao.MeetingRoomsDao;
import com.lemon.soft.bookingroom.entity.MeetingRoomsEntity;
import com.lemon.soft.bookingroom.util.PageUtils;
import com.lemon.soft.bookingroom.util.Query;
import com.lemon.soft.bookingroom.util.R;
import com.lemon.soft.bookingroom.vo.updateRoomStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.lemon.soft.bookingroom.dao.RoomTimeStatusDao;
import com.lemon.soft.bookingroom.entity.RoomTimeStatusEntity;
import com.lemon.soft.bookingroom.service.RoomTimeStatusService;
import org.springframework.transaction.annotation.Transactional;


@Service("roomTimeStatusService")
public class RoomTimeStatusServiceImpl extends ServiceImpl<RoomTimeStatusDao, RoomTimeStatusEntity> implements RoomTimeStatusService {
    @Autowired
    private MeetingRoomsDao meetingRoomService;
    @Override
    public R queryPage() {
        LambdaQueryWrapper<RoomTimeStatusEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomTimeStatusEntity::getIsDelete, 0).ne(RoomTimeStatusEntity::getStatus, "空闲");
        List<RoomTimeStatusEntity> collect = this.list(queryWrapper).stream().map(roomTimeStatus -> {
            MeetingRoomsEntity meetingRoom = meetingRoomService.selectById(roomTimeStatus.getRoomId());
            roomTimeStatus.setMeetingRoom(meetingRoom);
            return roomTimeStatus;
        }).collect(Collectors.toList());


        return R.ok().put("data", collect);
    }

    @Override
    public Boolean isRoomAvailable(Integer roomId, LocalDateTime startTime, LocalDateTime endTime) {
        return null;
    }
@Transactional
    @Override
    public String updateRoomStatus(updateRoomStatusVo vo) {




        // 3. 查询时间段状态记录
        LambdaQueryWrapper<RoomTimeStatusEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomTimeStatusEntity::getStatusId, vo.getStatusId());
        RoomTimeStatusEntity statusRecord = this.getOne(queryWrapper);

        if (statusRecord == null) {
//            throw new BusinessException("未找到该时间段的状态记录");
            return "未找到该时间段的状态记录";
        }

        if ( vo.getStatus().equals("维护"))
    {
        statusRecord.setStartTime(LocalDateTime.now());
        statusRecord.setEndTime(LocalDateTime.now().plusDays(60));
    }
        // 4. 更新状态
        statusRecord.setStatus(vo.getStatus());
        statusRecord.setUpdateTime(LocalDateTime.now());
        this.updateById(statusRecord);
        return "success";
    }

}